package pos.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import pos.service.SysData;

@Controller
@RequestMapping("syndata")
public class SynDataController {
	
	
	@Autowired
	private SysData syn;
	
	
	@RequestMapping(value="syntools")
	@ResponseBody
	public String Syn(HttpServletRequest re,HttpServletResponse rp){
		
		int currpage=Integer.parseInt(re.getParameter("currpage"));
		int pagesize=Integer.parseInt(re.getParameter("pagesize"));
		String field=re.getParameter("field");
		String tbl=re.getParameter("tbl");
		String keyid=re.getParameter("keyid");
	  //List<Map<String,Object>> ls=syn.SynData(tbl, keyid, currpage, pagesize,field);
		
			List<Map<String,Object>> ls=null;
		
		if (!EhcacheUtil.getInstance().Exists("ekey","list")){
			 ls=synservice.getData(tbl, keyid, currpage, pagesize);	
			EhcacheUtil.getInstance().put("list", "ekey", ls);
			System.out.println("第一次");
			List<Map<String,Object>> lss=(List<Map<String, Object>>) EhcacheUtil.getInstance().get("list","ekey");
			
			for(int i=0;i<lss.size();i++){
			
				System.out.println(lss.get(i).get("Name"));
				
			}
			
			//JSON.parseArray(lss.toString(), Employee.class);
			
			
			
			
			 System.out.println("第二次拿缓存bbbb");
			
		}else{
			 ls=(List<Map<String, Object>>) EhcacheUtil.getInstance().get("list","ekey");
			 System.out.println("第二次拿缓存");
		}
		
		
		
		
		
		
		
		JSONObject json =new JSONObject();
		json.put("ls", ls);		
		return JSONObject.toJSONString(json);
	}
	
	@RequestMapping(value="getpagecount")
	@ResponseBody
	public String Synpagecount(HttpServletRequest re,HttpServletResponse rp){
		
		//int currpage=Integer.parseInt(re.getParameter("currpage"));
		int pagesize=Integer.parseInt(re.getParameter("pagesize"));
		String tbl=re.getParameter("tbl");
		//String keyid=re.getParameter("keyid");
		int totalpage=syn.pagecount(tbl, pagesize);
	/*	Map<String,Object> m=new HashMap<String,Object>();
		m.put("totalpage", totalpage);
		List<Map<String,Object>> ls=new ArrayList<Map<String,Object>>();
		ls.add(m); */
		JSONObject json =new JSONObject();
		json.put("totalpage", totalpage);		
		return JSONObject.toJSONString(json);
	}
	
	
	

}
